<!-- 渠道信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="渠道名称">
                            <a-input v-model="channelName" allow-clear placeholder="请输入渠道名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="渠道标识">
                            <a-input v-model="channelTag" allow-clear placeholder="请输入渠道标识" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="鉴权状态">
                            <a-select v-model="authOnOff" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="强制鉴权状态">
                            <a-select v-model="isMustAuth" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="状态">
                            <a-select v-model="openOnOff" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button type="primary" style="margin-right: 20px" @click="getData()">查询</a-button>
                            <a-button type="" icon="plus" @click="add()">添加</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size,}" size="small" :scroll="{ x: 1500, }">
            <div slot="supportBank" slot-scope="supportBank">
                <ellipsis :length="18" tooltip>{{ supportBank }}</ellipsis>
            </div>
            <div slot="notSupportBank" slot-scope="notSupportBank">
                <ellipsis :length="18" tooltip>{{ notSupportBank }}</ellipsis>
            </div>
            <div slot="authOnOff" slot-scope="authOnOff">
                <div v-for="(item,i) in statusList" :key="item.id">
                    <a-tag v-if="item.id == authOnOff" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="isMustAuth" slot-scope="isMustAuth">
                <div v-for="(item,i) in statusList" :key="item.id">
                    <a-tag v-if="item.id == isMustAuth" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="openOnOff" slot-scope="openOnOff">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="openOnOff == '1' ? true : false" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="update(record)">修改</a-button>
                <a-popconfirm
                    title="是否确认删除当前配置?"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="del(record)"
                >
                    <a-button class="errorColor" type="link">删除</a-button>
                </a-popconfirm>
            </div>
        </a-table>

        <a-modal
            title="添加、修改渠道信息"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="渠道名称">
                        <a-input v-model="item.channelName" placeholder="请输入渠道名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="渠道标识">
                        <a-input v-model="item.channelTag"  placeholder="请输入渠道标识" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单日消费最大笔数">
                        <a-input v-model="item.dayConsumeMaxCount" placeholder="请输入单日消费最大笔数" suffix="笔" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单笔消费最大金额">
                        <a-input v-model="item.oneConsumeMaxAmount" placeholder="请输入单笔消费最大金额" suffix="元" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单笔消费最小金额">
                        <a-input v-model="item.oneConsumeMinAmount" placeholder="请输入单笔消费最小金额" suffix="元" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单日还款最大笔数">
                        <a-input v-model="item.dayRepaymentMaxCount" placeholder="请输入单日还款最大笔数" suffix="笔" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单笔还款最大金额">
                        <a-input v-model="item.oneRepaymentMaxAmount" placeholder="请输入单笔还款最大金额" suffix="元" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="单笔还款最小金额">
                        <a-input v-model="item.oneRepaymentMinAmount" placeholder="请输入单笔还款最小金额" suffix="元" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="支持银行" help="支持银行，用 - 分隔">
                        <a-input v-model="item.supportBank" type="textarea" placeholder="请输入支持银行" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="不支持银行" help="不支持银行，用 - 分隔">
                        <a-input v-model="item.notSupportBank" type="textarea" placeholder="请输入不支持银行" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="鉴权状态">
                        <a-radio-group v-model="item.authOnOff" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                    <a-form-item label="是否强制鉴权">
                        <a-radio-group v-model="item.isMustAuth" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                    <a-form-item label="状态">
                        <a-radio-group v-model="item.openOnOff" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryCardChannelConfig , updateCardChannelConfig } from '@/api/product';
import { Ellipsis } from '@/components'
export default {
    components: {
        Ellipsis,
    },
    data () {
        return {
            brandId: this.global.brandId,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            channelName: '', // 渠道名称
            channelTag: '', // 渠道标识
            authOnOff: undefined, // 是否开启鉴权
            isMustAuth: undefined, // 是否强制鉴权
            openOnOff: undefined,
            statusList: [
                { id: 1, name: '开启' },
                { id: 0, name: '关闭' },
            ],
            columns,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            confirmLoading: false,
            isAdd: true,
            visible: false,
            item: {
                'id': '',
                'channelName': '', // 渠道名称
                'channelTag': '', // 渠道标识
                'dayConsumeMaxCount': '', // 单日消费最大笔数
                'dayRepaymentMaxCount': '', // 单日还款最大笔数
                'oneConsumeMinAmount': '', // 单笔最小消费金额	
                'oneConsumeMaxAmount': '', // 单笔消费最大金额	
                'oneRepaymentMinAmount': '', // 单笔还款最小金额	
                'oneRepaymentMaxAmount': '', // 单笔还款最大金额		
                'supportBank': '', // 支持银行,用 - 分隔
                'notSupportBank': '', // 不支持银行,用 - 分隔
                'authOnOff': '1', // 鉴权开启状态
                'openOnOff': '1', // 状态，1：可用，0：不可用
                'isMustAuth': '0', // 是否强制鉴权
            },
        };
    },
    async created(){
        try {
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询渠道配置
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'channelName':_this.channelName,
                'channelTag':_this.channelTag,
                'authOnOff': _this.authOnOff,
                'isMustAuth': _this.isMustAuth,
                'openOnOff': _this.openOnOff,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryCardChannelConfig(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.channelName = '';
            this.item.channelTag = '';
            this.item.dayConsumeMaxCount = '';
            this.item.dayRepaymentMaxCount = '';
            this.item.oneConsumeMinAmount = '';
            this.item.oneConsumeMaxAmount = '';
            this.item.oneRepaymentMinAmount = '';
            this.item.oneRepaymentMaxAmount = '';
            this.item.supportBank = '';
            this.item.notSupportBank = '';
            this.item.authOnOff = 0;
            this.item.openOnOff = 1
            this.item.isMustAuth = 0
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.isAdd = false;
            this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(_this.item.channelName == ''){
                _this.$message.warning('请输入渠道名称')
                return;
            }
            if(_this.item.channelTag == ''){
                _this.$message.warning('请输入渠道标识')
                return;
            }
            if(_this.item.dayConsumeMaxCount === ''){
                _this.$message.warning('单日消费最大笔数')
                return;
            }
            if(_this.item.dayRepaymentMaxCount == ''){
                _this.$message.warning('单日还款最大笔数')
                return;
            }
            if(_this.item.oneConsumeMinAmount == ''){
                _this.$message.warning('单笔最小消费金额')
                return;
            }
            if(_this.item.oneConsumeMaxAmount == ''){
                _this.$message.warning('单笔消费最大金额')
                return;
            }
            if(_this.item.oneRepaymentMinAmount == ''){
                _this.$message.warning('单笔还款最小金额')
                return;
            }
            if(_this.item.oneRepaymentMaxAmount == ''){
                _this.$message.warning('单笔还款最大金额')
                return;
            }
            if(_this.item.supportBank == ''){
                _this.$message.warning('支持银行,用 - 分隔')
                return;
            }
            const params = {
                'id': _this.item.id,
                'channelName': _this.item.channelName,
                'channelTag': _this.item.channelTag,
                'isMustAuth': _this.item.isMustAuth,
                'authOnOff': _this.item.authOnOff,
                'openOnOff': _this.item.openOnOff,
                'dayConsumeMaxCount': _this.item.dayConsumeMaxCount,
                'dayRepaymentMaxCount': _this.item.dayRepaymentMaxCount,
                'oneConsumeMinAmount': _this.item.oneConsumeMinAmount,
                'oneConsumeMaxAmount': _this.item.oneConsumeMaxAmount,
                'oneRepaymentMinAmount': _this.item.oneRepaymentMinAmount,
                'oneRepaymentMaxAmount': _this.item.oneRepaymentMaxAmount,
                'supportBank': _this.item.supportBank,
                'notSupportBank': _this.item.notSupportBank,
            }
            updateCardChannelConfig(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'isDelete': '1'
            }
            updateCardChannelConfig(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>
</style>